package com.mk.legoscans2.activities;

import java.util.List;

import com.mk.legoscans2.LegoScans3Application;
import com.mk.legoscans2.R;
import com.mk.legoscans2.Scan;
import com.mk.legoscans2.actionbar.ActionBarActivity;
import com.mk.legoscans2.helpers.AccountAlertDialogHelper;
import com.mk.legoscans2.helpers.AccountDialogOnSelectItemAction;
import com.mk.legoscans2.helpers.FavoritesScanListActivitAccountDialogOnSelectItemAction;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class FavoritesScanListActivity extends ActionBarActivity {
	
	List<String> _favorites;
	ScanListFragment _scanListFragment;
	Context _context;
	
	DownloadFavoritesTask _downloadFavoritesTask;
	
	Handler _handler = new Handler(new Handler.Callback() {
		
		public boolean handleMessage(Message msg) {
			
			String selectedAccount = msg != null && msg.obj != null ? msg.obj.toString() : LegoScans3Application.AccountService.getAccount();
			_scanListFragment.setAccount(selectedAccount);
			
			_downloadFavoritesTask = new DownloadFavoritesTask(selectedAccount);
			_downloadFavoritesTask.execute();
			
			return true;
		}
	});
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.scans);
		
		_context = this;
		
		setTitle(R.string.favorites_title);
		
		_scanListFragment = (ScanListFragment)getSupportFragmentManager().findFragmentById(R.id.browse_fragment_container);
		
		_handler.sendEmptyMessage(0);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.favorites, menu);
        
        return super.onCreateOptionsMenu(menu);
    }
	
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch(item.getItemId())
		{
			case R.id.favorites_menu_account:
				AccountDialogOnSelectItemAction action = new FavoritesScanListActivitAccountDialogOnSelectItemAction(_handler, LegoScans3Application.AccountService.getAccount());
				AlertDialog d = AccountAlertDialogHelper.getDialog(_context, action);
				d.show();
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		LegoScans3Application.TempScans = null;
		super.onDestroy();
	}
	
	private class DownloadScansTask extends AsyncTask<String, Void, List<Scan>> {

		protected List<Scan> doInBackground(String... params) {
			return LegoScans3Application.ApiRepository.getScansByIds(_favorites);
		}
		
		protected void onPostExecute(List<Scan> result) {
			LegoScans3Application.TempScans = result;
			View scanListFragmentView = _scanListFragment.getView();

			if(scanListFragmentView != null)
			{
				_scanListFragment.updateList();
				scanListFragmentView.setVisibility(View.VISIBLE);
			}
		}
	}
	
	private class DownloadFavoritesTask extends AsyncTask<String, Void, List<String>> {

		String _selectedAccount;
		
		public DownloadFavoritesTask(String selectedAccount){
			_selectedAccount = selectedAccount;
		}
		
		@Override
		protected void onPreExecute() {
			_scanListFragment.setIsLoading(true);
		}
		
		protected List<String> doInBackground(String... params) {
			return LegoScans3Application.FavoritesService.getFavorites(_selectedAccount);
		}
		
		protected void onPostExecute(List<String> result) {
			
			_favorites = result;
			
			View scanListFragmentView = _scanListFragment.getView();
			TextView scansMessage = (TextView)findViewById(R.id.scans_message);
			
			if(scanListFragmentView != null && scansMessage != null)
			{
				if(_favorites.size() == 0)
				{
					scanListFragmentView.setVisibility(View.GONE);
					scansMessage.setText(R.string.favorites_no_scans_message);
					scansMessage.setVisibility(View.VISIBLE);
				}
				else{
					new DownloadScansTask().execute();
					scansMessage.setVisibility(View.GONE);
					scanListFragmentView.setVisibility(View.VISIBLE);
				}
			}
		}
	}
}
